package com.project.weimi.activity;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.SharedPreferencesHelper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 个人资料Activity
 * 
 * @author Administrator
 *
 */
public class PersonalInfoActivity extends BaseActivity {

	private Context mContext;
	private ImageView img_face;
	private TextView tv_complete, tv_nickName, tv_accountSafe, tv_sex, tv_age, tv_marriage, tv_address;
	private RelativeLayout rl_nickName, rl_accountSafe, rl_sex, rl_age, rl_marriage, rl_address;
	String nickName;
	String sex;
	String age;
	String marriageState;
	String address;
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_personal_info, "个人资料");

		initView();

	}

	private void initView() {
		mContext = this;
		img_face = (ImageView) findViewById(R.id.img_face);
		tv_complete = (TextView) findViewById(R.id.tv_complete);
		tv_nickName = (TextView) findViewById(R.id.tv_nickName);
		tv_accountSafe = (TextView) findViewById(R.id.tv_accountSafe);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		tv_age = (TextView) findViewById(R.id.tv_age);
		tv_marriage = (TextView) findViewById(R.id.tv_marriage);
		tv_address = (TextView) findViewById(R.id.tv_address);
		rl_nickName = (RelativeLayout) findViewById(R.id.rl_nickName);
		rl_accountSafe = (RelativeLayout) findViewById(R.id.rl_accountSafe);
		rl_sex = (RelativeLayout) findViewById(R.id.rl_sex);
		rl_age = (RelativeLayout) findViewById(R.id.rl_age);
		rl_marriage = (RelativeLayout) findViewById(R.id.rl_marriage);
		rl_address = (RelativeLayout) findViewById(R.id.rl_address);

		getValuesFromSharedPreferenceHelper();
		
		// 监听
		SetClickListener.setClickListener(listener, rl_nickName, rl_accountSafe, 
				rl_sex, rl_age, rl_marriage,rl_address);

	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = null;

			switch (v.getId()) {
			case R.id.rl_nickName: // 昵称
				intent = new Intent(mContext, ChangeNickNameActivity.class);
				intent.putExtra("nickName", baseNickName);
				startActivityForResult(intent, 0);
				break;
			case R.id.rl_accountSafe: // 账号安全
				intent = new Intent(mContext, AccountSafetyActivity.class);
				startActivity(intent);
				break;
			case R.id.rl_sex: // 性别
				intent = new Intent(mContext, SexActivity.class);
				intent.putExtra("sex", baseSex);
				startActivityForResult(intent, 1);
				break;
			case R.id.rl_age: // 年龄
				intent = new Intent(mContext, AgeActivity.class);
				intent.putExtra("age", baseAge);
				startActivityForResult(intent, 2);
				break;
			case R.id.rl_marriage: // 婚姻状况
				intent = new Intent(mContext, MarriageActivity.class);
				intent.putExtra("sign", baseSign);
				startActivityForResult(intent, 3);
				break;
			case R.id.rl_address: // 地址
				intent = new Intent(mContext, AddressActivity.class);
				intent.putExtra("address", baseAddress);
				startActivityForResult(intent, 4);
				break;
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (null != data) {
			nickName = data.getStringExtra("nickName");
			sex = data.getStringExtra("sex");
			age = data.getStringExtra("age");
			marriageState = data.getStringExtra("marriageState");
			address = data.getStringExtra("address");
			SharedPreferencesHelper sp = new SharedPreferencesHelper(mContext, Constant.USER_INFO);

			switch (requestCode) {
			case 0: // 昵称
				if (TextUtils.isEmpty(nickName)) {
					tv_nickName.setText("待完善");
					tv_nickName.setTextColor(Color.parseColor("#FF0000"));
				} else {
					tv_nickName.setText(nickName);
					tv_nickName.setTextColor(Color.parseColor("#313131"));
				}
				sp.put("nickName", nickName);

				break;
			case 1: // 性别
				if (TextUtils.isEmpty(sex)) {
					tv_sex.setText("待完善");
					tv_sex.setTextColor(Color.parseColor("#FF0000"));
				} else {
					tv_sex.setText(sex);
					tv_sex.setTextColor(Color.parseColor("#313131"));
				}
				sp.put("sex", sex);
				
				break;
			case 2: // 年龄
				tv_age.setText(age);
				tv_age.setTextColor(Color.parseColor("#313131"));
				
				sp.put("age", age);
				
				break;
			case 3: // 婚姻状况
				if(TextUtils.isEmpty(marriageState)){
					tv_marriage.setText("待完善");
					tv_marriage.setTextColor(Color.parseColor("#FF0000"));
				} else {
					tv_marriage.setText(marriageState);
					tv_marriage.setTextColor(Color.parseColor("#313131"));
				}
				sp.put("sign", marriageState);
				
				break;
			case 4: // 地址 
				if(TextUtils.isEmpty(address)){
					tv_address.setText("待完善");
					tv_address.setTextColor(Color.parseColor("#FF0000"));
				} else {
					tv_address.setText(address);
					tv_address.setTextColor(Color.parseColor("#313131"));
				}
				sp.put("address", address);
				
				break;
			}
		}
	}

	private void getValuesFromSharedPreferenceHelper() {
//		SharedPreferencesHelper sp = new SharedPreferencesHelper(mContext, Constant.USER_INFO);
//		nickName = (String) sp.get("nickName", "");
//		sex = (String) sp.get("sex", "");
		
		// 昵称
		if(!TextUtils.isEmpty(baseNickName)){
			tv_nickName.setText(baseNickName);
			tv_nickName.setTextColor(Color.parseColor("#313131"));
		}
		
		// 性别
		if(!TextUtils.isEmpty(baseSex)){
			//Log.e("TAG", "onCreate---PersonalInfoActivity性别非空=="+baseSex);
			tv_sex.setText(baseSex);
			tv_sex.setTextColor(Color.parseColor("#313131"));
		}
		
		// 年龄
		if(!TextUtils.isEmpty(baseAge)){
			//Log.e("TAG", "onCreate---PersonalInfoActivity年龄非空=="+baseAge);
			tv_age.setText(baseAge);
			tv_age.setTextColor(Color.parseColor("#313131"));
		}
		
		// 婚姻状况
		if(!TextUtils.isEmpty(baseSign)){
			//Log.e("TAG", "onCreate---PersonalInfoActivity婚姻非空=="+baseSign);
			tv_marriage.setText(baseSign);
			tv_marriage.setTextColor(Color.parseColor("#313131"));
		}
		
		// 地址
		if(!TextUtils.isEmpty(baseAddress)){
			//Log.e("TAG", "onCreate---PersonalInfoActivity地址非空=="+baseAddress);
			tv_address.setText(baseAddress);
			tv_address.setTextColor(Color.parseColor("#313131"));
		}
		
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}

}
